//
//  ShoppingVC.swift
//  AliBaiChuan
//
//  Created by 郑东喜 on 2017/2/21.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit

// MARK:- 购物车数组
var _shopCarArray : NSMutableArray = NSMutableArray()

class ShoppingVC: BaseWebView {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        if _shopCarArray.count > 0 {
            self.weView.loadRequest(URLRequest.init(url: URL.init(string: (_shopCarArray.lastObject as? String)!)!))
        } else {
            
        }
    }
}



// MARK:- UIWebViewDelegate
extension ShoppingVC {
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        let urlString = request.url?.absoluteString
        
        //根据单机状态跳转
        //根据单机状态跳转
        if navigationType == .linkClicked || navigationType == .formResubmitted || navigationType.rawValue == 0 {
            
            self.url = urlString!
            
            if _shopCarArray.count > 0 && _shopCarArray.lastObject as? String == urlString  {
                
                _shopCarArray.removeLastObject()
                return true
            }
            
            
            _shopCarArray.add(urlString as Any)
            
            self.jumpReVi(replaceURL: urlString!)
            
            return false
        } else {
            return true
        }
        
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",navigationType.rawValue)
        
        return true
    }
}

// MARK:- 跳转替身网页
extension ShoppingVC {
    
    /// 替身网页跳转
    ///
    /// - Parameter url: 替身网页跳转的url
    func jumpReVi(replaceURL url: String) -> Void {
        
        let vvv : ShopReplaceVC = ShopReplaceVC()
        vvv.url = self.url
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(ShopReplaceVC(), animated: true)
    }
}

